package com.ejuornal.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ejuornal.bean.Comment;
import com.ejuornal.bean.Page;
import com.ejuornal.dao.CommentDao;
import com.ejuornal.service.CommentService;

@Component("commentService")
public class CommentServiceImpl implements CommentService {
	private CommentDao commentDao;
	public void add(Comment comment) {
		String sql = "insert into t_comment (content,commDate,score,opinion,userId,type,typeId) values (?,?,?,?,?,?,?)";
		commentDao.add(sql, new Object[] { comment.getContent(), 
						comment.getCommDate(),
						comment.getScore(), comment.getOpinion(),
						comment.getUserId(), comment.getType(),
						comment.getTypeId() });
	}

	public void delete(int... commentId) {
		String sql = "delete from t_comment where commentId = ?";
		commentDao.batchDelete(sql, commentId);
	}

	public Comment detailInfoComment(int id) {
		return commentDao.detailInfoComment(id);
	}
	
	public Page<Comment> findByTypeId(int current_record, String url, String type, int typeId) {
		String hsql = "WHERE type = ? AND typeId = ?";

		return commentDao.find(current_record, url, hsql, new Object[]{ type, typeId });
	}
	
	@Override
	public List<Comment> findByPerPeriodicalId(int userId, int perPeriodicalId) {
		String sql = "SELECT c.commentId, c.content, c.commDate, c.typeId, c.type, a.title, a.perPeriodicalId " +
					"FROM t_comment AS c INNER JOIN t_artical AS a ON c.typeId = a.articalId AND c.type = 1 AND a.status=3 " +
					"INNER JOIN t_per_periodical AS pp ON a.perPeriodicalId = pp.perPeriodicalId AND pp.status = 1 " +
					"INNER JOIN t_periodical AS p ON pp.periodicalId = p.periodicalId AND p.status = 1 " +
					"WHERE p.userId=? AND pp.perPeriodicalId=? ORDER BY a.articalId DESC, c.commentId DESC";

		return commentDao.findComment(sql, new Object[]{ userId, perPeriodicalId });
	}

	@Override
	public List<Comment> findByEditoryChiefId(int editoryChiefId) {
		String sql = "SELECT c.commentId, c.content, c.commDate, c.typeId, c.type, a.title, a.perPeriodicalId " +
					"FROM t_comment AS c INNER JOIN t_artical AS a ON c.typeId = a.articalId AND c.type = 1 AND a.status=3 " +
					"INNER JOIN t_per_periodical AS pp ON a.perPeriodicalId = pp.perPeriodicalId AND pp.status = 1 " +
					"INNER JOIN t_periodical AS p ON pp.periodicalId = p.periodicalId AND p.status = 1 " +
					"WHERE p.userId=? ORDER BY a.articalId DESC, c.commentId DESC";
		
		return commentDao.findComment(sql, new Object[]{ editoryChiefId });
	}
	
	

	@Override
	public Page<Comment> findAll(int current_record, String url) {
		String sql_1 = "SELECT COUNT(*) FROM t_periodical AS p INNER JOIN t_per_periodical AS pp ON p.periodicalId=pp.periodicalId " + 
				"INNER JOIN t_artical AS a ON pp.perPeriodicalId=a.perPeriodicalId INNER JOIN t_comment AS c ON a.articalId=c.typeId AND c.type=1 ORDER BY c.typeId DESC,c.commentId DESC";
		String sql_2 = "SELECT a.*,c.commentId,c.content AS ccontent,c.commDate,c.score FROM t_periodical AS p INNER JOIN t_per_periodical AS pp ON p.periodicalId=pp.periodicalId " + 
				"INNER JOIN t_artical AS a ON pp.perPeriodicalId=a.perPeriodicalId INNER JOIN t_comment AS c ON a.articalId=c.typeId AND c.type=1 ORDER BY c.typeId DESC,c.commentId DESC LIMIT "+ current_record +",10";

		return commentDao.find(current_record, url, sql_1, sql_2, null);
	}

	public CommentDao getCommentDao() {
		return commentDao;
	}

	@Resource(name = "commentDao")
	public void setCommentDao(CommentDao commentDao) {
		this.commentDao = commentDao;
	}

    @Override
    public Page<Comment> findByDate(int current_record, String url, Date beginDate, Date endDate) {
        String sql_1 = "SELECT COUNT(*) FROM t_periodical AS p INNER JOIN t_per_periodical AS pp ON p.periodicalId=pp.periodicalId " + 
                "INNER JOIN t_artical AS a ON pp.perPeriodicalId=a.perPeriodicalId INNER JOIN t_comment AS c ON a.articalId=c.typeId AND c.type=1 AND DATEDIFF(commDate, ?)>0 AND DATEDIFF(commDate, ?) <0";
        String sql_2 = "SELECT a.*,c.commentId,c.content AS ccontent,c.commDate,c.score FROM t_periodical AS p INNER JOIN t_per_periodical AS pp ON p.periodicalId=pp.periodicalId " + 
                "INNER JOIN t_artical AS a ON pp.perPeriodicalId=a.perPeriodicalId INNER JOIN t_comment AS c ON a.articalId=c.typeId AND c.type=1 AND DATEDIFF(commDate, ?)>0 AND DATEDIFF(commDate, ?) <0 ORDER BY c.typeId DESC,c.commentId DESC LIMIT "+ current_record +",10";

        return commentDao.find(current_record, url, sql_1, sql_2, new Object[]{beginDate, endDate});
    }
}
